﻿using System;
using Microsoft.Xna.Framework;
using Terraria.Localization;
using Terraria.Net.Sockets;

namespace Terraria
{
	// Token: 0x02000047 RID: 71
	public class RemoteClient
	{
		// Token: 0x060006A4 RID: 1700 RVA: 0x000092A0 File Offset: 0x000074A0
		public bool IsConnected()
		{
			return this.Socket != null && this.Socket.IsConnected();
		}

		// Token: 0x060006A5 RID: 1701 RVA: 0x00369B14 File Offset: 0x00367D14
		public void SpamUpdate()
		{
			if (!Netplay.spamCheck)
			{
				this.SpamProjectile = 0f;
				this.SpamDeleteBlock = 0f;
				this.SpamAddBlock = 0f;
				this.SpamWater = 0f;
				return;
			}
			if (this.SpamProjectile > this.SpamProjectileMax)
			{
				NetMessage.BootPlayer(this.Id, NetworkText.FromKey("Net.CheatingProjectileSpam", new object[0]));
			}
			if (this.SpamAddBlock > this.SpamAddBlockMax)
			{
				NetMessage.BootPlayer(this.Id, NetworkText.FromKey("Net.CheatingTileSpam", new object[0]));
			}
			if (this.SpamDeleteBlock > this.SpamDeleteBlockMax)
			{
				NetMessage.BootPlayer(this.Id, NetworkText.FromKey("Net.CheatingTileRemovalSpam", new object[0]));
			}
			if (this.SpamWater > this.SpamWaterMax)
			{
				NetMessage.BootPlayer(this.Id, NetworkText.FromKey("Net.CheatingLiquidSpam", new object[0]));
			}
			this.SpamProjectile -= 0.4f;
			if (this.SpamProjectile < 0f)
			{
				this.SpamProjectile = 0f;
			}
			this.SpamAddBlock -= 0.3f;
			if (this.SpamAddBlock < 0f)
			{
				this.SpamAddBlock = 0f;
			}
			this.SpamDeleteBlock -= 5f;
			if (this.SpamDeleteBlock < 0f)
			{
				this.SpamDeleteBlock = 0f;
			}
			this.SpamWater -= 0.2f;
			if (this.SpamWater < 0f)
			{
				this.SpamWater = 0f;
			}
		}

		// Token: 0x060006A6 RID: 1702 RVA: 0x000092B7 File Offset: 0x000074B7
		public void SpamClear()
		{
			this.SpamProjectile = 0f;
			this.SpamAddBlock = 0f;
			this.SpamDeleteBlock = 0f;
			this.SpamWater = 0f;
		}

		// Token: 0x060006A7 RID: 1703 RVA: 0x00369CA4 File Offset: 0x00367EA4
		public static void CheckSection(int playerIndex, Vector2 position, int fluff = 1)
		{
			int sectionX = Netplay.GetSectionX((int)(position.X / 16f));
			int sectionY = Netplay.GetSectionY((int)(position.Y / 16f));
			int num = 0;
			for (int i = sectionX - fluff; i < sectionX + fluff + 1; i++)
			{
				for (int j = sectionY - fluff; j < sectionY + fluff + 1; j++)
				{
					if (i >= 0 && i < Main.maxSectionsX && j >= 0 && j < Main.maxSectionsY && !Netplay.Clients[playerIndex].TileSections[i, j])
					{
						num++;
					}
				}
			}
			if (num > 0)
			{
				int num2 = num;
				NetMessage.SendData(9, playerIndex, -1, Lang.inter[44].ToNetworkText(), num2, 0f, 0f, 0f, 0, 0, 0);
				Netplay.Clients[playerIndex].StatusText2 = Language.GetTextValue("Net.IsReceivingTileData");
				Netplay.Clients[playerIndex].StatusMax += num2;
				for (int k = sectionX - fluff; k < sectionX + fluff + 1; k++)
				{
					for (int l = sectionY - fluff; l < sectionY + fluff + 1; l++)
					{
						if (k >= 0 && k < Main.maxSectionsX && l >= 0 && l < Main.maxSectionsY && !Netplay.Clients[playerIndex].TileSections[k, l])
						{
							NetMessage.SendSection(playerIndex, k, l, false);
							NetMessage.SendData(11, playerIndex, -1, null, k, (float)l, (float)k, (float)l, 0, 0, 0);
						}
					}
				}
			}
		}

		// Token: 0x060006A8 RID: 1704 RVA: 0x00369E18 File Offset: 0x00368018
		public bool SectionRange(int size, int firstX, int firstY)
		{
			for (int i = 0; i < 4; i++)
			{
				int num = firstX;
				int num2 = firstY;
				if (i == 1)
				{
					num += size;
				}
				if (i == 2)
				{
					num2 += size;
				}
				if (i == 3)
				{
					num += size;
					num2 += size;
				}
				int sectionX = Netplay.GetSectionX(num);
				int sectionY = Netplay.GetSectionY(num2);
				if (this.TileSections[sectionX, sectionY])
				{
					return true;
				}
			}
			return false;
		}

		// Token: 0x060006A9 RID: 1705 RVA: 0x00369E74 File Offset: 0x00368074
		public void ResetSections()
		{
			for (int i = 0; i < Main.maxSectionsX; i++)
			{
				for (int j = 0; j < Main.maxSectionsY; j++)
				{
					this.TileSections[i, j] = false;
				}
			}
		}

		// Token: 0x060006AA RID: 1706 RVA: 0x00369EB0 File Offset: 0x003680B0
		public void Reset()
		{
			this.ResetSections();
			if (this.Id < 255)
			{
				Main.player[this.Id] = new Player(true);
			}
			this.TimeOutTimer = 0;
			this.StatusCount = 0;
			this.StatusMax = 0;
			this.StatusText2 = "";
			this.StatusText = "";
			this.State = 0;
			this.IsReading = false;
			this.PendingTermination = false;
			this.SpamClear();
			this.IsActive = false;
			NetMessage.buffer[this.Id].Reset();
			if (this.Socket != null)
			{
				this.Socket.Close();
			}
		}

		// Token: 0x060006AB RID: 1707 RVA: 0x000092E5 File Offset: 0x000074E5
		public void ServerWriteCallBack(object state)
		{
			NetMessage.buffer[this.Id].spamCount--;
			if (this.StatusMax > 0)
			{
				this.StatusCount++;
			}
		}

		// Token: 0x060006AC RID: 1708 RVA: 0x00369F54 File Offset: 0x00368154
		public void ServerReadCallBack(object state, int length)
		{
			if (!Netplay.disconnect)
			{
				if (length == 0)
				{
					this.PendingTermination = true;
				}
				else
				{
					if (Main.ignoreErrors)
					{
						try
						{
							NetMessage.ReceiveBytes(this.ReadBuffer, length, this.Id);
							goto IL_43;
						}
						catch
						{
							goto IL_43;
						}
					}
					NetMessage.ReceiveBytes(this.ReadBuffer, length, this.Id);
				}
			}
			IL_43:
			this.IsReading = false;
		}

		// Token: 0x04000CAB RID: 3243
		public ISocket Socket;

		// Token: 0x04000CAC RID: 3244
		public int Id;

		// Token: 0x04000CAD RID: 3245
		public string Name = "Anonymous";

		// Token: 0x04000CAE RID: 3246
		public bool IsActive;

		// Token: 0x04000CAF RID: 3247
		public bool PendingTermination;

		// Token: 0x04000CB0 RID: 3248
		public bool IsAnnouncementCompleted;

		// Token: 0x04000CB1 RID: 3249
		public bool IsReading;

		// Token: 0x04000CB2 RID: 3250
		public int State;

		// Token: 0x04000CB3 RID: 3251
		public int TimeOutTimer;

		// Token: 0x04000CB4 RID: 3252
		public string StatusText = "";

		// Token: 0x04000CB5 RID: 3253
		public string StatusText2;

		// Token: 0x04000CB6 RID: 3254
		public int StatusCount;

		// Token: 0x04000CB7 RID: 3255
		public int StatusMax;

		// Token: 0x04000CB8 RID: 3256
		public bool[,] TileSections = new bool[Main.maxTilesX / 200 + 1, Main.maxTilesY / 150 + 1];

		// Token: 0x04000CB9 RID: 3257
		public byte[] ReadBuffer;

		// Token: 0x04000CBA RID: 3258
		public float SpamProjectile;

		// Token: 0x04000CBB RID: 3259
		public float SpamAddBlock;

		// Token: 0x04000CBC RID: 3260
		public float SpamDeleteBlock;

		// Token: 0x04000CBD RID: 3261
		public float SpamWater;

		// Token: 0x04000CBE RID: 3262
		public float SpamProjectileMax = 100f;

		// Token: 0x04000CBF RID: 3263
		public float SpamAddBlockMax = 100f;

		// Token: 0x04000CC0 RID: 3264
		public float SpamDeleteBlockMax = 500f;

		// Token: 0x04000CC1 RID: 3265
		public float SpamWaterMax = 50f;
	}
}
